<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Warriors Name Generator</title>
    <!-- 使用CDN加载Bootstrap和jQuery -->
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootswatch/4.4.1/cosmo/bootstrap.min.css?v=1.0">
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js?v=1.0"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js?v=1.0"></script>

    <style>
        body {
            font-family: '宋体', sans-serif;
            background: linear-gradient(135deg, #ececec, #f5f5f5);
            background-size: cover;
            background-repeat: no-repeat;
            background-attachment: fixed;
            padding: 20px;
        }
        .navbar {
            background-color: #fff;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border-radius: 12px;
        }
        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            margin-top: 20px;
        }
        h1 {
            color: #004085;
            font-weight: bold;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
        }
        .btn-primary {
            background-color: #007bff;
            border: none;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border-radius: 12px;
        }
        .btn-primary:hover {
            background-color: #0056b3;
        }
        .custom-switch {
            margin-bottom: 10px;
        }
        .custom-switch .custom-control-label {
            color: #004085;
        }
    </style>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const script3 = document.createElement('script');
            script3.src = "../data/en.js?v=1.0";
            document.body.appendChild(script3);

            script3.onload = function () {
                // JavaScript代码部分
            };
        });
    </script>
</head>
<body>
    <header class="mb-5">
        <nav class="navbar navbar-expand-lg navbar-light">
            <a class="navbar-brand" href="./">Warriors Name Generator</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="../en/">Return to Home</a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Language
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="../zh-cn/">中文（中国大陆）</a>
                            <a class="dropdown-item" href="../zh-tw/">中文（台灣）</a>
                            <a class="dropdown-item" href="../en/">English</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="https://gitlab.com/Qinshihuang233/warriors-name">Project Address</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="./about-en/">About</a>
                    </li>
                </ul>
            </div>
        </nav>
    </header>

    <main role="main" class="container">
        <h1 class="mb-4 text-center" id="displayName"></h1>
        <div class="text-center">
            <button class="btn btn-primary" type="button" onclick="generateName()">Next</button>
        </div>
        <hr class="my-4" />
        <div class="custom-control custom-switch">
            <input type="checkbox" class="custom-control-input" id="includeUnderage" onclick="changeIncludeUnderage()">
            <label class="custom-control-label" for="includeUnderage">Allow kit and apprentice names</label>
        </div>
        <div class="custom-control custom-switch">
            <input type="checkbox" class="custom-control-input" id="ignoreWeighting" onclick="changeIgnoreWeighting()">
            <label class="custom-control-label" for="ignoreWeighting">Ignore affix weighting</label>
        </div>
        <p class="mt-3">This open source project：<a href="https://gitlab.com/Qinshihuang233/warriors-name">Open Source Project Address</a></p>
    </main>

    <!-- 延迟加载JavaScript文件 -->
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const script1 = document.createElement('script');
            script1.src = "https://code.jquery.com/jquery-3.4.1.slim.min.js";
            script1.integrity = "sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n";
            script1.crossOrigin = "anonymous";
            document.body.appendChild(script1);

            const script2 = document.createElement('script');
            script2.src = "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js";
            script2.integrity = "sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6";
            script2.crossOrigin = "anonymous";
            document.body.appendChild(script2);

            const script3 = document.createElement('script');
            script3.src = "../data/en.js";
            document.body.appendChild(script3);

            script3.onload = function () {
                const basePrefixes = data.prefixes;
                const baseSuffixes = data.suffixes;
                const totalApprentices = data.apprentices;
                const totalKits = data.kits;
                const unweightedPrefixes = [...new Set(basePrefixes)];
                const unweightedSuffixes = [...new Set(baseSuffixes)];
                let prefixes = basePrefixes;
                let suffixes = baseSuffixes;

                function changeIncludeUnderage() {
                    document.getElementById("ignoreWeighting").disabled =
                        document.getElementById("includeUnderage").checked;
                }

                function changeIgnoreWeighting() {
                    document.getElementById("includeUnderage").disabled =
                        document.getElementById("ignoreWeighting").checked;
                    prefixes = document.getElementById("ignoreWeighting").checked ?
                        basePrefixes : unweightedPrefixes;
                    suffixes = document.getElementById("ignoreWeighting").checked ?
                        baseSuffixes : unweightedSuffixes;
                }

                function generateName() {
                    const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
                    let suffix;
                    if (document.getElementById("includeUnderage").checked) {
                        const s = Math.floor(Math.random() *
                            (suffixes.length + totalApprentices + totalKits));
                        if (s < suffixes.length) {
                            suffix = suffixes[s];
                        } else if (s < suffixes.length + totalApprentices) {
                            suffix = "paw";
                        } else {
                            suffix = "kit";
                        }
                    } else {
                        suffix = suffixes[Math.floor(Math.random() * suffixes.length)];
                    }
                    const name = prefix.charAt(0).toUpperCase() + prefix.slice(1) + suffix;
                    document.getElementById("displayName").innerHTML = name;
                }

                document.getElementById("includeUnderage").addEventListener('click', changeIncludeUnderage);
                document.getElementById("ignoreWeighting").addEventListener('click', changeIgnoreWeighting);
                document.querySelector('.btn-primary').addEventListener('click', generateName);

                generateName();
            };
        });
    </script>
</body>
</html>
